package bb.lanxing.lib.devices.sync;

import com.garmin.fit.Mesg;
import com.garmin.fit.SessionMesg;
import bb.lanxing.lib.devices.core.sync.AbstractSyncManager;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.DeviceFileStatus;
import bb.lanxing.lib.devices.sync.FitManager;
import java.io.File;

public abstract class AbstractFitSyncManager extends AbstractSyncManager implements FitManager.FitManagerCallback {
    private DeviceFile currentDeviceFile;
    protected FitManager fitManager;
    protected FitProcessor fitProcessor;

    @Override
    public void abortAll() {
    }

    protected abstract boolean syncImpl(DeviceFile deviceFile);

    protected void uploadToServer(DeviceFile deviceFile) {
    }

    public void init(String str, FitProcessor fitProcessor) {
        FitManager fitManager = this.fitManager;
        if (fitManager != null) {
            fitManager.release();
        }
        this.fitManager = new FitManager(str, this);
        this.fitProcessor = fitProcessor;
    }

    public DeviceFile getCurrentDeviceFile() {
        return this.currentDeviceFile;
    }

    @Override
    public boolean isSynchronising() {
        return super.isSynchronising() || this.currentDeviceFile != null;
    }

    @Override
    protected boolean onSync(DeviceFile deviceFile) {
        if (syncImpl(deviceFile)) {
            this.currentDeviceFile = deviceFile;
            notifySyncStatus(deviceFile, DeviceFileStatus.STATUS_SYNCING.getStatus());
            return true;
        }
        this.currentDeviceFile = null;
        notifySyncStatus(deviceFile, DeviceFileStatus.STATUS_SYNC_FAIL.getStatus());
        return false;
    }

    @Override
    public void notifySyncStatus(DeviceFile deviceFile, int i, String str, boolean z) {
        super.notifySyncStatus(deviceFile, i, str, z);
        DeviceFileStatus vStatus = DeviceFileStatus.valueOf(i);
        if (vStatus != null) {
            if (vStatus == DeviceFileStatus.STATUS_SYNC_FAIL || vStatus == DeviceFileStatus.STATUS_SYNCED) {
                currentDeviceFile = pop();
            }
        }
    }

    @Override
    public String getPath(long j) {
        FitManager fitManager = this.fitManager;
        if (fitManager != null) {
            return fitManager.getPath(String.valueOf(j));
        }
        return null;
    }

    @Override
    public void onFitReceived(FitTrans fitTrans) {
        FitProcessor fitProcessor;
        DeviceFile deviceFileById = getDeviceFileById(fitTrans.getId());
        if (deviceFileById == null) {
            return;
        }
        if (this.fitManager != null && (fitProcessor = this.fitProcessor) != null) {
            fitProcessor.onStart(fitTrans);
            this.fitManager.decode(fitTrans);
            return;
        }
        notifySyncStatus(deviceFileById, DeviceFileStatus.STATUS_SYNC_FAIL.getStatus());
    }

    @Override
    public void onMesg(FitTrans fitTrans, Mesg mesg) {
        if (mesg == null) {
            DeviceFile deviceFileById = getDeviceFileById(fitTrans.getId());
            this.fitProcessor.onComplete(fitTrans);
            uploadToServer(deviceFileById);
            notifySyncStatus(deviceFileById, DeviceFileStatus.STATUS_SYNCED.getStatus());
            return;
        }
        this.fitProcessor.onMesg(mesg);
    }

    @Override
    public void onSport(FitTrans fitTrans, SessionMesg sessionMesg) {
        FitProcessor fitProcessor = this.fitProcessor;
        if (fitProcessor != null) {
            fitProcessor.onSport(sessionMesg);
        }
    }

    @Override
    public void onError(FitTrans fitTrans, Throwable th) {
        th.printStackTrace();
        DeviceFile deviceFileById = getDeviceFileById(fitTrans.getId());
        if (deviceFileById == null) {
            return;
        }
        notifySyncStatus(deviceFileById, DeviceFileStatus.STATUS_SYNC_FAIL.getStatus());
        if (fitTrans.path == null) {
            return;
        }
        File file = new File(fitTrans.path);
        if (!file.exists()) {
            return;
        }
        file.delete();
    }
}
